home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 176-200 / scopedisk194 / newgadsv1.0 / newgads.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-20  |  7.9 KB  |  435 lines

  1. /*
  2. ** NewGads - makes the system gadgets look a little better
  3. **
  4. ** Version 1.0
  5. **
  6. ** Author: Edward Hutchins
  7. **
  8. ** Based on the other program, 3dlook, which does the same thing
  9. ** This program will probably die a horrible death under 1.4!
  10. */
  11.  
  12. #define INTUITIONPRIVATE
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17. #include <exec/types.h>
  18. #include <exec/exec.h>
  19. #include <intuition/intuition.h>
  20. #include <intuition/intuitionbase.h>
  21. #include <proto/all.h>
  22.  
  23. /*
  24. ** defines
  25. */
  26.  
  27. #define DRAGPATTERN GADGETCOUNT
  28. #define DOTPATTERN GADGETCOUNT+1
  29.  
  30. #define MAX_DATA 2048
  31.  
  32. #define ADDRSYSGAD(r,i) (IntuitionBase->SysGadgets[r][i])
  33.  
  34. #define DePrint(x)
  35.  
  36. /*
  37. ** Global variables
  38. */
  39.  
  40. struct IntuitionBase            *IntuitionBase;
  41.  
  42. typedef struct
  43. {
  44.     WORD        Res;
  45.     WORD        Index;
  46.     int            Num;
  47.     USHORT        Offset1;
  48.     SHORT        Depth1;
  49.     USHORT        ImgOffset;
  50.     USHORT        Offset2;
  51.     SHORT        Depth2;
  52. } GadList;
  53.  
  54. GadList            NewGads[20] =
  55. {
  56.     { HIRESGADGET, UPFRONTGADGET, },
  57.     { HIRESGADGET, DOWNBACKGADGET, },
  58.     { HIRESGADGET, SIZEGADGET, },
  59.     { HIRESGADGET, CLOSEGADGET, },
  60.     { HIRESGADGET, SUPFRONTGADGET, },
  61.     { HIRESGADGET, SDOWNBACKGADGET, },
  62.     { LOWRESGADGET, UPFRONTGADGET, },
  63.     { LOWRESGADGET, DOWNBACKGADGET, },
  64.     { LOWRESGADGET, SIZEGADGET, },
  65.     { LOWRESGADGET, CLOSEGADGET, },
  66.     { LOWRESGADGET, SUPFRONTGADGET, },
  67.     { LOWRESGADGET, SDOWNBACKGADGET, },
  68.     { RESCOUNT, 0, },
  69. };
  70.  
  71. char            *GadNames[] =
  72. {
  73. "UPFRONTGADGET",
  74. "DOWNBACKGADGET",
  75. "SIZEGADGET",
  76. "CLOSEGADGET",
  77. "DRAGGADGET",
  78. "SUPFRONTGADGET",
  79. "SDOWNBACKGADGET",
  80. "SDRAGGADGET",
  81. "DRAGPATTERN",
  82. "DOTPATTERN",
  83. NULL
  84. };
  85.  
  86. USHORT            ImgData[MAX_DATA];
  87. USHORT            Offset;
  88.  
  89. /*
  90. ** getstr - get a string of input
  91. */
  92.  
  93. void getstr( FILE *fp, char *buf )
  94. {
  95.     char            *p;
  96.  
  97.     fgets( buf, 80, fp );
  98.     p = buf + strlen( buf );
  99.     while (p > buf && *p < ' ') *p-- = '\0';
  100. DePrint(("Got string %s\n", buf));
  101. }
  102.  
  103. /*
  104. ** getnum - get a number
  105. */
  106.  
  107. SHORT getnum( FILE *fp )
  108. {
  109.     char            buf[80];
  110.  
  111.     fgets( buf, 80, fp );
  112. DePrint(("Got num string %s\n", buf));
  113.     return( (SHORT)atoi( buf ) );
  114. }
  115.  
  116. /*
  117. ** gethex - get a hex number
  118. */
  119.  
  120. USHORT gethex( FILE *fp )
  121. {
  122.     char            buf[80];
  123.     char            *p;
  124.     USHORT            num;
  125.  
  126.     fgets( buf, 80, fp );
  127. DePrint(("Got hex string %s ", buf));
  128.     num = 0;
  129.     p = buf;
  130.     while (isxdigit(*p))
  131.     {
  132.         num = (num << 4) + (isdigit(*p) ? (*p - '0') : (toupper(*p) - ('A' - 10)));
  133.         ++p;
  134.     }
  135. DePrint(("Converted: %x\n", num));
  136.     return( num );
  137. }
  138.  
  139. /*
  140. ** printimage - print the contents of an image structure
  141. */
  142.  
  143. void printimage( struct Image *img )
  144. {
  145.     int            x, y, w, wu, h, d, bit;
  146.     USHORT        *Bitplanes, *bitplane;
  147.     char        Bits[6*16+1];
  148.     char        *p;
  149.  
  150.     if (img == NULL) return;
  151.     
  152.     printf( "%d\n%d\n%d\n", img->Width, img->Height, img->Depth );
  153.  
  154.     w = ((img->Width + 15) & ~15);
  155.     wu = w / 16;
  156.     h = wu * img->Height;
  157.     Bitplanes = img->ImageData;
  158.  
  159.     for (y = 0; y < img->Height; ++y)
  160.     {
  161.         p = Bits;
  162.         for (x = 0; x < w; ++x)
  163.         {
  164.             *p = '\0';
  165.             bit = 1 << (15-(x & 15));
  166.             bitplane = Bitplanes + (x / 16);
  167.             for (d = 0; d < img->Depth; ++d)
  168.             {
  169.                 *p |= (*bitplane & bit) ? (1 << d) : 0;
  170.                 bitplane += h;
  171.             }
  172.             *p += '0';
  173.             ++p;
  174.         }
  175.         *p = '\0';
  176.         printf( "%s\n", Bits );
  177.         Bitplanes += wu;
  178.     }
  179. }
  180.  
  181. /*
  182. ** printgad - print the specs on a gadget
  183. */
  184.  
  185. void printgad( struct Gadget *gad )
  186. {
  187.     int                num;
  188.  
  189.     num = (gad->SelectRender == NULL) ? 1 : 2;
  190.     printf( "%d\n", num );
  191.     printimage( (struct Image *)gad->GadgetRender );
  192.     if (num == 2) printimage( (struct Image *)gad->SelectRender );
  193. }
  194.  
  195. /*
  196. ** PrintSysGads - print the standard gadgets
  197. */
  198.  
  199. void PrintSysGads( void )
  200. {
  201.     GadList            *glist = NewGads;
  202.     struct Gadget            *gad;
  203.     int                t;
  204.  
  205.     while (glist->Res != RESCOUNT)
  206.     {
  207.         gad = ADDRSYSGAD( glist->Res, glist->Index );
  208.         printf( "%s\n%d\n", GadNames[glist->Index], glist->Res );
  209.         printgad( gad );
  210.         ++glist;
  211.     }
  212.  
  213.     printf( "%s\n", GadNames[DRAGPATTERN] );
  214.  
  215.     for (t = 0; t < 8; ++t)
  216.         printf( "%4x\n", IntuitionBase->apattern[t] );
  217.  
  218.     printf( "%s\n", GadNames[DOTPATTERN] );
  219.  
  220.     for (t = 0; t < 4; ++t)
  221.         printf( "%4x\n", IntuitionBase->bpattern[t] );
  222. }
  223.  
  224. /*
  225. ** GetGadIndex - convert an ASCII name into an index
  226. */
  227.  
  228. WORD GetGadIndex( FILE *hFile )
  229. {
  230.     char            Name[80];
  231.     WORD            t = 0;
  232.  
  233.     getstr( hFile, Name );
  234.  
  235.     while (GadNames[t] != NULL)
  236.     {
  237.         if (strcmp( Name, GadNames[t] ) == 0)
  238.         {
  239. DePrint(("Got %s as %d\n", Name, t));
  240.             return( t );
  241.         }
  242.         ++t;
  243.     }
  244.     return( -1 );
  245. }
  246.  
  247. /*
  248. ** GetImage - get an image definition
  249. */
  250.  
  251. void GetImage( FILE *hFile, SHORT *dpth, struct Image *img )
  252. {
  253.     WORD            x, y, w, wu, h, d, depth, bit, planesize;
  254.     USHORT            *Bitplanes, *bitplane;
  255.     char            Bits[80];
  256.     char            *p;
  257.  
  258.     w = getnum( hFile );
  259.     h = getnum( hFile );
  260.     depth = getnum( hFile );
  261. DePrint(("Image is (%d,%d)×%d\n", w, h, depth));
  262.     *dpth = depth;
  263.  
  264.     if ((img != NULL) && (w != img->Width || h != img->Height))
  265.         printf( "NewGads warning: nonstandard gadget size!\n" );
  266.  
  267.     Bitplanes = &(ImgData[Offset]);
  268.     w = (w + 15) & ~15;
  269.     wu = w / 16;
  270.     planesize = h * wu;
  271.     Offset += planesize * depth;
  272.  
  273.     for (y = 0; y < h; ++y)
  274.     {
  275.         getstr( hFile, Bits );
  276. DePrint(("Read %s\n", Bits));
  277.         for (p = Bits; *p; ++p) *p -= '0';
  278.         p = Bits;
  279.  
  280.         for (x = 0; x < w; ++x)
  281.         {
  282.             bit = 1 << (15-(x & 15));
  283.             bitplane = Bitplanes + (x / 16);
  284.             for (d = 0; d < depth; ++d)
  285.             {
  286.                 *bitplane |= (*p & (1 << d)) ? bit : 0;
  287.                 bitplane += planesize;
  288.             }
  289.             ++p;
  290.         }
  291.         Bitplanes += wu;
  292.     }
  293. DePrint(("Done with bitmap!\n"));
  294. }
  295.  
  296. /*
  297. ** GetGadget - get a gadget definition
  298. */
  299.  
  300. void GetGadget( FILE *hFile, GadList *gl, WORD Index )
  301. {
  302.     struct Gadget            *gad;
  303.  
  304.     gl->Index = Index;
  305.     gl->Res = getnum( hFile );
  306.     gl->Num = getnum( hFile );
  307.  
  308. DePrint(("Working on res %d, num %d\n", gl->Res, gl->Num));
  309.      gad = ADDRSYSGAD( gl->Res, gl->Index );
  310.  
  311.     gl->Offset1 = Offset;
  312.     GetImage( hFile, &(gl->Depth1), (struct Image *)gad->GadgetRender );
  313.     if (gl->Num == 2)
  314.     {
  315.         gl->ImgOffset = Offset;
  316.         *((struct Image *)&(ImgData[Offset])) = *((struct Image *)gad->GadgetRender);
  317.         Offset += (1 + sizeof(struct Image)) >> 1;
  318.         gl->Offset2 = Offset;
  319.         GetImage( hFile, &(gl->Depth2), NULL );
  320.     }
  321. }
  322.  
  323. /*
  324. ** SetImage - set the image structure
  325. */
  326.  
  327. void SetImage( struct Image *img, USHORT offset, SHORT depth, USHORT *ChipMem )
  328. {
  329.     img->ImageData = ChipMem + offset;
  330.     img->Depth = depth;
  331.     img->PlanePick = (1 << depth) - 1;
  332.     img->PlaneOnOff = 0;
  333. }
  334.  
  335. /*
  336. ** SetGadget - set a gadget structure
  337. */
  338.  
  339. void SetGadget( GadList *gl, USHORT *ChipMem )
  340. {
  341.     struct Gadget            *gad;
  342.  
  343.     gad = ADDRSYSGAD( gl->Res, gl->Index );
  344.     SetImage( (struct Image *)gad->GadgetRender, gl->Offset1, gl->Depth1, ChipMem );
  345.     if (gl->Num == 2)
  346.     {
  347.         gad->Flags = (gad->Flags & ~GADGHIGHBITS) | GADGHIMAGE;
  348.         gad->SelectRender = (APTR)(ChipMem + gl->ImgOffset);
  349.         SetImage( (struct Image *)gad->SelectRender, gl->Offset2, gl->Depth2, ChipMem );
  350.     }
  351. }
  352.  
  353. /*
  354. ** SetPattern - set a pattern
  355. */
  356.  
  357. void SetPattern( FILE *hFile, USHORT *pat, int Max )
  358. {
  359.     int                t;
  360.  
  361.     for (t = 0; t < Max; ++t) pat[t] = gethex( hFile );
  362. }
  363.  
  364. /*
  365. ** SetSysGads - set the system gadgets from a definition file
  366. */
  367.  
  368. void SetSysGads( char *DefFile )
  369. {
  370.     FILE            *hFile;
  371.     WORD            Index;
  372.     GadList            *glist = NewGads;
  373.     GadList            *gl = NewGads;
  374.     char            *ChipMem;
  375.     ULONG            ChipMemSize;
  376.  
  377.     hFile = fopen( DefFile, "r" );
  378.     if (hFile == 0) return;
  379.  
  380.     Offset = 0;
  381.  
  382.     while ((Index = GetGadIndex( hFile )) != -1)
  383.     {
  384.         switch (Index)
  385.         {
  386.         case DRAGPATTERN:
  387.             SetPattern( hFile, IntuitionBase->apattern, 8 );
  388.             break;
  389.         case DOTPATTERN:
  390.             SetPattern( hFile, IntuitionBase->bpattern, 4 );
  391.             break;
  392.         default:
  393.             GetGadget( hFile, glist++, Index );
  394.             break;
  395.         }
  396.     }
  397.  
  398.     fclose( hFile );
  399.  
  400.     /* no gadget images */
  401.     if (Offset == 0) return;
  402.  
  403.     /* allocate the chip memory for the images */
  404.     ChipMemSize = Offset * sizeof( USHORT );
  405.     ChipMem = AllocMem( ChipMemSize, MEMF_CHIP );
  406.     if (ChipMem == NULL)
  407.     {
  408.         printf( "Allocation failed!\n" );
  409.         return;
  410.     }
  411.  
  412.     memcpy( ChipMem, (char *)ImgData, ChipMemSize );
  413.  
  414.     Forbid();
  415.     Disable();
  416.  
  417.     while (gl != glist) SetGadget( gl++, (USHORT *)ChipMem );
  418.  
  419.     Permit();
  420.     Enable();
  421. }
  422.  
  423. /*
  424. ** main - do it!
  425. */
  426.  
  427. void main( int argc, char *argv[] )
  428. {
  429.     IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 33 );
  430.     if (IntuitionBase == NULL) exit( 0 );
  431.  
  432.     if (argc != 2) PrintSysGads();
  433.     else SetSysGads( argv[1] );
  434. }
  435.